import Vue from 'vue'
import VueRouter from 'vue-router'
import { Toast } from "vant";
Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push;

VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject)
    return originalPush.call(this, location, onResolve, onReject);
  return originalPush.call(this, location).catch(err => err);
};
const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import('../views/Home.vue'),
    children: [
      {
        path: 'recommend',
        alias: '',
        name: 'Recommend',
        component: () => import('../views/Recommend.vue')
      },
      {
        path: 'ranking',
        alias: '',
        name: 'RanKing',
        redirect: '/ranking/riman',
        component: () => import('../views/RanKing.vue'),
        children: [
          {
            path: 'riman',
            name: 'Riman',
            component: () => import('../views/Riman.vue')
          },
          {
            path: 'guoman:type',
            name: 'Guoman',
            component: () => import('../views/Guoman.vue')
          },
          {
            path: 'gratis:type',
            name: 'Gratis',
            component: () => import('../views/Gratis.vue')
          }
        ]
      },
      {
        path: 'classify',
        name: 'ClassIfy',
        component: () => import('../views/ClassIfy.vue'),
      },
      {
        path: 'mine',
        name: 'Mine',
        component: () => import('../views/Mine.vue'),
        beforeEnter: (to, from, next) => {
          if (sessionStorage.getItem("user")) {
            next()
          } else {
            Toast('账号未登录')
            setTimeout(() => {
              next({
                path: '/login'
              })
            })
          }
        }
      },
      {
        path: 'load',
        name: 'Load',
        component: () => import('../components/Load.vue')
      }
    ]
  },
  {
    path: '/detail',
    name: 'Detail',
    component: () => import('../views/Detail.vue'),
    beforeEnter: (to, from, next) => {
      if (sessionStorage.getItem("user")) {
        next()
      } else {
        Toast('账号未登录')
        setTimeout(() => {
          next({
            path: '/login'
          })
        }, 2000)
      }
    }
  },
  {
    path: '/read',
    name: 'Read',
    component: () => import('../views/Read.vue')
  },
  {
    path: '/vip',
    name: 'Vip',
    component: () => import('../components/Vip.vue')
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/registered',
    name: 'Registered',
    component: () => import('../views/Registered.vue')
  },
  {
    path: '/collection',
    name: 'Collection',
    component: () => import('../views/Collection.vue')
  },
]

const router = new VueRouter({
  routes
})

export default router
